using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class RangeValidator : System.Web.UI.WebControls.RangeValidator, IContent, IConfigurationAware
	{
        private IRangeValidatorConfiguration configuration;
		private string configurationId;

		private string textKey;
		private string errorMessageKey;

        protected IDataBinder binder;

		private String contentPlaceHolderId;

        private bool checkMinimimTypeValue;
        private bool checkMaximumTypeValue;

        #region Properties

	    public bool CheckMinimimTypeValue
	    {
	        get { return checkMinimimTypeValue; }
	        set { checkMinimimTypeValue = value; }
	    }

	    public bool CheckMaximumTypeValue
	    {
	        get { return checkMaximumTypeValue; }
	        set { checkMaximumTypeValue = value; }
	    }

	    public IRangeValidatorConfiguration Configuration
	    {
            get { return configuration; }
            set { configuration = value; }
        }

        public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

        IConfiguration IConfigurationAware.Configuration
		{
			get { return configuration; }
			set { configuration = (IRangeValidatorConfiguration) value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.RangeValidator")]
		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string ErrorMessageKey
		{
			get { return errorMessageKey; }
			set { errorMessageKey = value; }
        }

        #endregion 

        public RangeValidator(IConfiguration configuration): base ()
		{
			Configuration = (IRangeValidatorConfiguration) configuration;
		}

		protected void Build()
		{
			ID=Configuration.Id;			
			TextKey = configuration.TextKey;
		    
			if (Configuration == null)
			{
				return;
			}

            if (configuration.CssClass != null)
            {
                CssClass = configuration.CssClass;
            }

			ControlToValidate = Configuration.ControlToValidate;
			ErrorMessageKey = Configuration.ErrorMessageKey;						
			Display = Configuration.Display;
            ValidationGroup = configuration.ValidationGroup;
            Type = Configuration.Type;

            if (Configuration.MinimumValueBindPath == null)		    
                MinimumValue = Configuration.MinimumValue;

            if (Configuration.MaximumValueBindPath == null)
                MaximumValue = Configuration.MaximumValue;
		    
            EnableClientScript = Configuration.ClientScriptingEnabled;
		    
		}

		protected override void OnInit(EventArgs e)
		{
            binder = new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
			if (Configuration == null)
			{
				Configuration = (IRangeValidatorConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			}
			
		    Build();
			
		    
			base.OnInit (e);
		}


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Unbind();
        }

        protected override void OnPreRender(EventArgs e)
        {            
            Bind();
            Text = configuration.GetLocalizedPostfixLabel(TextKey, Text);
            ErrorMessage = configuration.GetLocalizedPostfixLabel(ErrorMessageKey, ErrorMessage);
            IRangeValidatorConfiguration validatorConfiguration = Configuration as IRangeValidatorConfiguration;                        
            base.OnPreRender(e);   
        }

        protected virtual void Bind()
	    {
            binder.Bind("MinimumValue", Configuration.DataTranslator);
            binder.Bind("MaximumValue", Configuration.DataTranslator);
            binder.Bind("ErrorMessageKey");	
	        binder.Bind("Enabled");            
	    }

        protected virtual void Unbind()
	    {
            binder.Unbind(new string[] { "MinimumValue", "MaximumValue", "ErrorMessageKey", "Enabled" });
	    }

	    
	}
}
